﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class D_TreatmentType
    {
        public bool CreateOneTreatmentType(TreatmentType_Info objTreatmentType_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("TreatmentType_Insert", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"TreatmentTypeID", objTreatmentType_Info.TreatmentTypeID));
            command.Parameters.Add(new SqlParameter(@"TreatmentTypeName", objTreatmentType_Info.TreatmentTypeName));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //**********************************************************************************       
        public bool EditOneTreatmentType(TreatmentType_Info objTreatmentType_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("TreatmentType_Update", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TreatmentTypeID", objTreatmentType_Info.TreatmentTypeID));
            command.Parameters.Add(new SqlParameter("@TreatmentTypeName", objTreatmentType_Info.TreatmentTypeName));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public bool RemoveOneTreatmentType(int TreatmentTypeID)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("TreatmentType_Delete", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"TreatmentTypeID", TreatmentTypeID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public DataTable GetAllTreatmentType()
        {
            DataSet ds = new DataSet();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("TreatmentType_SelectAll", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                objConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return ds.Tables[0];
        }
        //******************************************************************************       
        public TreatmentType_Info GetOneTreatmentType(int TreatmentTypeID)
        {
            TreatmentType_Info objTreatmentType_Info = new TreatmentType_Info();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("TreatmentType_SelectByID", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"TreatmentTypeID", TreatmentTypeID));
            try
            {
                objConnection.Open();
                SqlDataReader ordTreatmentType = command.ExecuteReader(); //OjectRecordDatareader
                ordTreatmentType.Read();
                if (ordTreatmentType.HasRows)
                {
                    if (ordTreatmentType["TreatmentTypeID"] != DBNull.Value)
                    {
                        objTreatmentType_Info.TreatmentTypeID = Convert.ToInt16(ordTreatmentType["TreatmentTypeID"]);
                    }
                    if (ordTreatmentType["TreatmentTypeName"] != DBNull.Value)
                    {
                        objTreatmentType_Info.TreatmentTypeName = Convert.ToString(ordTreatmentType["TreatmentTypeName"]);
                    }
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return objTreatmentType_Info;
        }
    }
}
